package com.myoffice.biz.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myoffice.biz.SysFunBiz;
import com.myoffice.dao.BaseDaoI;
import com.myoffice.model.TSysfun;
import com.myoffice.pageModel.DataGrid;
import com.myoffice.pageModel.Sysfun;

@Service("sysFunBiz")
public class SysFunBizImpl implements SysFunBiz {
	
	private BaseDaoI<TSysfun> tSysFunDao=null;
	
	public BaseDaoI<TSysfun> gettSysFunDao() {
		return tSysFunDao;
	}
	@Autowired
	public void settSysFunDao(BaseDaoI<TSysfun> tSysFunDao) {
		this.tSysFunDao = tSysFunDao;
	}

	public List<Sysfun> getTree(Sysfun sysfun) {
		List<Sysfun> sList=new ArrayList<Sysfun>();
		List<TSysfun> tsList =new ArrayList<TSysfun>();
		String hql="";
		Map<String,Object> params=new HashMap<String, Object>();
		if (sysfun.getId() == null) {
			hql="from TSysfun ts where ts.TSysfun is null";
		}else{
			hql="from TSysfun ts where ts.TSysfun.id=:id";
			params.put("id", sysfun.getId());
		}
		tsList= this.tSysFunDao.finds(hql, params);
		
		if(tsList!=null&&tsList.size()>0){
			for(TSysfun t:tsList){
				Sysfun m=new Sysfun();
				BeanUtils.copyProperties(t, m);
				Map<String,Object> attributes =new HashMap<String,Object>();
				attributes.put("url", t.getNodeurl());
				m.setAttributes(attributes);
				Set<TSysfun> set=t.getTSysfuns();
				if(set!=null&&!set.isEmpty()){
					m.setState("closed");
				}else{
					m.setState("open");
				}
				sList.add(m);
			}
		}
		
		return sList;
	}
	
	public DataGrid gridTree(Sysfun sysfun) {
		DataGrid d=new DataGrid();
		String hql="from TSysfun ts";
		String totalHql="select count(*) "+hql;
		List<TSysfun> ts= this.tSysFunDao.find(hql,sysfun.getPage(),sysfun.getRows());
		List<Sysfun> sy=new ArrayList<Sysfun>();
		if(ts!=null&&ts.size()>0){
			for(TSysfun t:ts){
				Sysfun s =new Sysfun(); 
				BeanUtils.copyProperties(t, s);
				if(t.getTSysfun()!=null){
					s.setPid(t.getTSysfun().getId());	
				}
				sy.add(s);
			}
		}
		d.setRows(sy);
		d.setTotal(this.tSysFunDao.count(totalHql));
		
		return d;
	}
	public Sysfun addCdgl(Sysfun sysFun) {
		TSysfun tss=new TSysfun();
		tss= this.tSysFunDao.get("from TSysfun ts where ts.text=?",new Object[]{sysFun.getText()});
		if(tss==null){
			TSysfun ts=new TSysfun();
			ts.setId(UUID.randomUUID().toString());
			ts.setNodeurl(sysFun.getNodeurl());
			ts.setText(sysFun.getText());
			if(sysFun.getPid()!=null&&"".equals(sysFun.getPid())){
				TSysfun t=this.tSysFunDao.get(TSysfun.class,sysFun.getPid());
				if(t!=null){
					ts.setTSysfun(t);
				}
			}
			
			Serializable id=this.tSysFunDao.save(ts);
			if(id!=null&&!("".equals(id))){
				BeanUtils.copyProperties(ts, sysFun);
				return sysFun;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	public void removeCdgl(String ids) {
		String id[]=ids.split(",");
		for(int i=0;i<id.length;i++){
			TSysfun ts=this.tSysFunDao.get(TSysfun.class, id[0]);
			this.tSysFunDao.delete(ts);
		}
	}
}
